import sys, re, operator

as_pair = set()
as_triple = {}
hijack_file = open('./anomalies.format')
for l in hijack_file:
  c = l[:-1].split('|') # time, pfx, home, bad_path, is_hijk
  if c[3]:
    if c[3].count(' ') == 2:
      ib = c[3].rindex(' ')
      first_two = c[3][:ib]
      last_one = c[3][ib+1:]
      st = as_triple.get(first_two)
      if not st:
        st = set()
        as_triple[first_two] = st
      st.add(last_one)
    else:
      as_pair.add(c[3])
hijack_file.close()

def add_policy(first_two, st_as_triple, asm, last_one):
  # parse last ones
  los = set()
  for lo in last_ones:
    if re.match('\s*', lo):
      pass
    elif lo == 'ANY' or lo == 'any':
      los.add('*')
    elif lo.startswith('AS'):
      los.add(lo[2:])
    elif lo.startswith('AS-'):
      los.add(lo)
    elif lo == 'NOT':
      los = None
      break
  if not los:
    return
  # deal with last ones
  st_all_asseg = all_asseg.find(first_two, None)
  if not st_all_asseg:
    st_all_asseg = set()
    all_asseg[first_two] = st_all_asseg
  if '*' in los:
    for a in st_all_triple:
      st_all_asseg.add(a)
  else:
    for lo in los:
      if lo.startswith('AS-'):
        st_all_asseg.add(lo)
      elif lo in st_as_triple:
        st_all_asseg.add(lo)

import re
all_pair = {}
all_asseg = {}
all_asset = {}
asn = ''
ass = ''
line = ''
for ln in sys.stdin:
  # line continue
  if (ln[0] == ' ' or ln[0] == '\t') and line and (asn or ass):
    ln = line + ln
    line = ln
  else:
    line = ''
  # ASN
  if ln.startswith('aut-num:'):
    asn_match = re.match('aut-num:\s*AS([0-9\./]+)\s*$', ln)
    if not asn_match:
      print >> sys.stderr, "%r" % ln
      break
    asn = asn_match.group(1)
  # import
  elif ln.startswith('import:'):
    if asn:
      if ('action' not in ln) or ('accept' not in ln):
        line = ln
      else:
        im_match = re.match('import:\s*from AS([0-9]+) \s*action [^;]+; \s*accept ([.\s]+)',
            ln, re.IGNORECASE)
        if not im_match:
          print >> sys.stderr, "%r" % ln
          break
        asm = im_match.group(1)
        first_two = '%s %s' % (asn, asm)
        if (first_two in as_pair) and (first_two not in all_pair):
          all_pair.add(first_two)
        st_as_triple = as_triple.get(first_two, None)
        if not st_as_triple:
          continue
        # last ones
        str_last_ones = im_match.group(2)
        if ' NOT ' in str_last_ones:
          continue
        last_ones = im_match.group(2).split(' ')
        add_policy(first_two, st_as_triple, asm, last_ones)
  # export
  elif ln.startswith('export:'):
    if asn:
      if 'annonuce' not in ln:
        line = ln
      else:
        ex_match = re.match('export:\s*to AS([0-9]+) \s*announce ([.\s]+)',
            ln, re.IGNORECASE)
        if not ex_match:
          print >> sys.stderr, "%r" % ln
          break
        asm = im_match.group(1)
        first_two = '%s %s' % (asm, asn)
        if (first_two in as_pair) and (first_two not in all_pair):
          all_pair.add(first_two)
        st_as_triple = as_triple.get(first_two)
        if not st_as_triple:
          continue
        # last one
        last_ones = ex_match.group(2).split(' ')
        los = set()
        for lo in last_ones:
          if lo == 'ANY' or lo == 'any':
            los.add('*')
          elif lo.startswith('AS'):
            los.add(lo[2:])
          elif lo.startswith('AS-'):
            los.add(lo)
          else:
            los = None
            break
        if not los:
          continue
        st = all_asseg.find(first_two, None)
        if not st:
          st = set()
          all_asseg[first_two] = st
        for lo in los:
          st.add(lo)
      line = ln
      pass
  # AS-SET
  elif ln.startswith('as-set:'):
    ass_match = re.match('aut-set:\s*(AS-\S+)\s*$', ln)
    if not ass_match:
      print >> sys.stderr, "%r" % ln
    ass = ass_match.group(1)
  # member
  elif ln.startswith('members:'):
    if ass:
      line = ln
      pass
  # new entry
  elif ln == '':
    asn = ''
    ass = ''

for pfx, AS in radb_origins.iteritems():
  print pfx, ' '.join(AS)

